<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.css"/>
    <style>
    	.aui-content.thumb {
    		margin-bottom: 0;
    	}
    	.aui-content {
    		background: #ffffff;
            overflow: hidden;
    	}
    	.count .count-btn {
            height: 43px;
            width: 43px;
            background: #ff9900;
            text-align: center;
            font-size: 1.5em;
            color: #fff;
            border-radius: 50%;
            margin: 0 auto;
        }
        .count .count-btn.disabled {
            background: #ccc;
        }
        .total {
            width: 100%;
            padding: 15px 0;
            text-align: center;
        }
        .total strong {
            color: #ff6600;
            font-size: 1.8em;

        }
        .total > small {
            padding-left: 10px;
        }
        .list {
            padding-top: 15px;
        }
        input[type="number"] {
            background: none;
            text-align: center;
            border-color: #ddd;
            border-radius: 20px;
            font-size: 1em;
        }
        .aui-content.userinfo {
            background: #ffffff;
            overflow: hidden;
            line-height: 48px;
            padding: 10px;
        }
        img.avatar {
            width: 48px;
            height: 48px;
            border-radius: 50%;
            display: inherit;
        }
        strong.price {
            font-size: 18px;
            color: #ff9933;
            padding: 0 5px;
        }
    </style>
</head>
<body>
    <div class="aui-content userinfo">
        <div class="aui-pull-left">我的钱包：<strong class="price" id="myPoint"></strong>分</div>
        <div class="aui-pull-right">
            <img src="../image/noavatar.gif" class="avatar" id="myAvatar" />
        </div>
    </div>
    <p class="aui-text-center">购买数量</p>
	<section class="aui-content list">
        <div class="aui-col-xs-12 count">
            <div class="aui-col-xs-3">
                <div class="count-btn disabled" tapmode onclick="minus()" id="minus"><i class="aui-iconfont aui-icon-move"></i></div>
            </div>
            <div class="aui-col-xs-6"><input type="number" id="quantity" class="aui-input" value="1" readonly /></div>
            <div class="aui-col-xs-3">
                <div class="count-btn" tapmode onclick="plus()" id="plus"><i class="aui-iconfont aui-icon-add"></i></div>
            </div>
        </div>
    </section>
    <p class="aui-clearfix aui-text-center">总价</p>
    <div class="total"><strong class="total" id="total">0</strong><small>分</small></div>
    <p class="aui-text-center">收货信息</p>
    <section class="aui-content">
        
        <ul class="aui-form" id="addressInput">
            <li class="aui-input-row">
                <div class="aui-input-addon">收货人姓名</div>
                <input type="text" name="text" id="name" class="aui-input" placeholder="请输入收货人姓名"/>
            </li>
            <li class="aui-input-row">
                <div class="aui-input-addon">收货人电话</div>
                <input type="text" name="text" id="mobile" class="aui-input" placeholder="请输入收货人电话"/>
            </li>
            <li class="aui-input-row">
                <div class="aui-input-addon">收货地址</div>
                <input type="text" name="text" id="address" class="aui-input" placeholder="请输入收货地址"/>
            </li>
            <li class="aui-btn-row">
                <div class="aui-btn aui-btn-warning" tapmode onclick="buy()" id="buyBtn">确认兑换</div>
            </li>
        </ul>
    </section>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    var goodsId,goodsPoint,userPoint;
	apiready = function(){
		api.parseTapmode();
        goodsId = api.pageParam.goodsId;
        getData();
        getUserInfo();
	}
    function getUserInfo(){
        var userid = $api.getStorage('userid');
        api.ajax({
            url: 'http://www.shalinet.com/App/Member/getUserInfo',
            method: 'post',
            cache:true,
            timeout: 30,
            headers:{
                'x-apicloud-appid':api.appId,
                'x-apicloud-appkey':'182E0D23-782D-44F1-1FB9-9CF487F3E326'
            },
            dataType: 'json',
            returnAll:false,
            data:{
                values:{
                    userid:userid
                }
            }
        },function(ret,err){
            if(ret){
                $api.text($api.byId("myPoint"),ret.point);
                if(ret.avatar){
                    $api.attr($api.byId("myAvatar"),'src',ret.avatar);
                }
                
            }
        });
    }
    function minus(){
        if($api.dom("#minus.disabled"))return;
        var _quantity = parseFloat($api.val($api.byId("quantity")));
        var newQuantity = _quantity-1;
        var newPriceTotal = parseFloat(goodsPoint*newQuantity);
        if(newQuantity<1){
            newQuantity = 1;
        }else{
            $api.val($api.byId("quantity"),newQuantity);
            if(newQuantity==1){
                $api.addCls($api.byId("minus"),'disabled');
            }
        }
        if(newPriceTotal>userPoint){
            $api.addCls($api.byId("plus"),'disabled');
            $api.addCls($api.byId("buyBtn"),'disabled');
            return;
        }else{
            $api.removeCls($api.byId("plus"),'disabled');
            $api.removeCls($api.byId("buyBtn"),'disabled');
        }
        $api.text($api.byId('total'),newPriceTotal);
    }
    function plus(){
        if($api.dom("#plus.disabled"))return;
        var _quantity = parseFloat($api.val($api.byId("quantity")));
        var newQuantity = _quantity+1;
        
        var newPriceTotal = parseFloat(goodsPoint*newQuantity);
        if(newPriceTotal>userPoint){
            $api.addCls($api.byId("plus"),'disabled');
            $api.addCls($api.byId("buyBtn"),'disabled');
            api.toast({
                msg: '您的积分不足',
                duration:2000,
                location: 'middle'
            });
            return;
        }else{
            $api.removeCls($api.byId("plus"),'disabled');
            $api.removeCls($api.byId("buyBtn"),'disabled');
        }
        $api.val($api.byId("quantity"),newQuantity);
        $api.text($api.byId('total'),newPriceTotal);
        if(newQuantity>1){
            $api.removeCls($api.byId("minus"),'disabled');
        }
    }
    function buy(){
        var userid = $api.getStorage('userid');
        if($api.dom("#buyBtn.disabled")){
            api.toast({
                msg: '您的积分不足',
                duration:2000,
                location: 'middle'
            });
        }else{
            var point = $api.text($api.byId("total"));
            var quantity = $api.val($api.byId("quantity"));
            var receive_name = $api.val($api.byId("name"));
            var receive_mobile = $api.val($api.byId("mobile"));
            var receive_address = $api.val($api.byId("address"));
            if(!point || !quantity || !receive_mobile || !receive_address || !receive_name){
                api.toast({
                    msg: '请输入完整信息',
                    duration:2000,
                    location: 'middle'
                });
            }else{
                api.ajax({
                    url: 'http://www.shalinet.com/App/Goods/goodsBuy',
                    method: 'post',
                    cache:true,
                    timeout: 30,
                    headers:{
                        'x-apicloud-appid':api.appId,
                        'x-apicloud-appkey':'182E0D23-782D-44F1-1FB9-9CF487F3E326'
                    },
                    dataType: 'json',
                    returnAll:false,
                    data:{
                        values:{
                            userid:userid,
                            goodsId:goodsId,
                            point:point,
                            quantity:quantity,
                            receive_name:receive_name,
                            receive_mobile:receive_mobile,
                            receive_address:receive_address
                        }
                    }
                },function(ret,err){
                    if(ret && ret.code==200){
                        api.sendEvent({
                            name:'refreshData'
                        })
                        api.alert({
                            title: '兑换成功',
                            msg: '恭喜您兑换成功，请等待发货',
                            buttons:['确定']
                        },function(ret,err){
                            if(ret.buttonIndex == 1){
                                api.closeWin({
                                    name: 'goods_buy_win'
                                });
                            }
                        });

                        
                    }else{
                        api.alert({
                            title: '抱歉',
                            msg: '兑换失败',
                            buttons:['确定']
                        },function(ret,err){
                            if(ret.buttonIndex == 1){
                            }
                        });
                    }
                    
                })
            }
        }
    }
    function getData(){
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '登录中...',
            text: '请稍后...',
            modal: false
        });
        var userid = $api.getStorage('userid');
        api.ajax({
            url: 'http://www.shalinet.com/App/Goods/goodsBuyInfo',
            method: 'post',
            cache:true,
            timeout: 30,
            headers:{
                'x-apicloud-appid':api.appId,
                'x-apicloud-appkey':'182E0D23-782D-44F1-1FB9-9CF487F3E326'
            },
            dataType: 'json',
            returnAll:false,
            data:{
                values:{
                    userid:userid,
                    goodsId:goodsId
                }
            }
        },function(ret,err){
            api.hideProgress();
            if(ret){
                if(ret.code==200){
                    $api.text($api.byId("total"),ret.goods_point);
                    goodsPoint = ret.goods_point;
                    userPoint = ret.user_point;
                }else{
                    api.alert({
                        title: '抱歉',
                        msg: '您的积分不足，赶快分享赚积分',
                        buttons:['确定']
                    },function(ret,err){
                        if(ret.buttonIndex == 1){
                            api.closeWin({
                                name: 'goods_buy_win'
                            });
                        }
                    });
                }
                
            }
            
        })
    }
</script>
</html>